import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';

/// Loading蒙层组件
class LoadingOverlay {
  static OverlayEntry? _overlayEntry;

  static void show({
    String? message,
    Color? backgroundColor,
    Color? indicatorColor,
    double? indicatorSize,
    TextStyle? textStyle,
    Duration? duration,
    bool dismissible = false,
    VoidCallback? onDismiss,
    BuildContext? context,
  }) {
    hide();

    _overlayEntry = OverlayEntry(
      builder:
          (context) => _LoadingWidget(
            message: message,
            backgroundColor: backgroundColor,
            indicatorColor: indicatorColor,
            indicatorSize: indicatorSize,
            textStyle: textStyle,
            duration: duration,
            dismissible: dismissible,
            onDismiss: onDismiss,
          ),
    );

    final overlayContext = context ?? Get.context;
    if (overlayContext != null) {
      try {
        final overlay = Overlay.of(overlayContext);
        overlay.insert(_overlayEntry!);
      } catch (e) {
        if (Get.key.currentContext != null) {
          final overlay = Overlay.of(Get.key.currentContext!);
          overlay.insert(_overlayEntry!);
        }
      }
    }
  }

  static void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }
}

/// Loading Widget
class _LoadingWidget extends StatefulWidget {
  final String? message;
  final Color? backgroundColor;
  final Color? indicatorColor;
  final double? indicatorSize;
  final TextStyle? textStyle;
  final Duration? duration;
  final bool dismissible;
  final VoidCallback? onDismiss;

  const _LoadingWidget({
    this.message,
    this.backgroundColor,
    this.indicatorColor,
    this.indicatorSize,
    this.textStyle,
    this.duration,
    this.dismissible = false,
    this.onDismiss,
  });

  @override
  State<_LoadingWidget> createState() => _LoadingWidgetState();
}

class _LoadingWidgetState extends State<_LoadingWidget> {
  @override
  void initState() {
    super.initState();
    if (widget.duration != null) {
      Future.delayed(widget.duration!, () {
        if (mounted) {
          LoadingOverlay.hide();
          widget.onDismiss?.call();
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
        onTap:
            widget.dismissible
                ? () {
                  LoadingOverlay.hide();
                  widget.onDismiss?.call();
                }
                : null,
        child: Container(
          color: widget.backgroundColor ?? Colors.black.withOpacity(0.5),
          child: Center(
            child: GestureDetector(
              onTap: () {},
              child: Container(
                padding: EdgeInsets.all(24.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.r),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.1),
                      blurRadius: 10.r,
                      offset: Offset(0, 4.h),
                    ),
                  ],
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SizedBox(
                      width: widget.indicatorSize ?? 40.w,
                      height: widget.indicatorSize ?? 40.w,
                      child: CircularProgressIndicator(
                        strokeWidth: 3.w,
                        valueColor: AlwaysStoppedAnimation<Color>(
                          widget.indicatorColor ?? Colors.blue,
                        ),
                      ),
                    ),
                    if (widget.message != null) ...[
                      SizedBox(height: 16.h),
                      Text(
                        widget.message!,
                        style:
                            widget.textStyle ??
                            TextStyle(
                              fontSize: 16.sp,
                              color: Colors.black87,
                              fontWeight: FontWeight.w500,
                            ),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
